<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="cl"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<c:set var="ctx" value="<%=request.getContextPath()%>"/>
<link href="${ctx}/css/gj.css" rel="stylesheet">
<body class="homepage">
   <!--登录窗口-->
  	<div class="modal fade" id="GJLogin" tabindex="-1" role="dialog"
       aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close"
                  data-dismiss="modal" aria-hidden="true">
            &times;
          </button>
          <h4 class="modal-title" id="myModalLabel">
            <img src="${ctx }/favicon/favicon.ico" style="margin-top: -5px;width: 50px;height:50px" />用户登录
          </h4>
        </div>
        <br class="modal-body">
        <!--表单-->
          <div class="form-group form-inline" style="margin-left: 180px">
          <font id="errormessage" style="color:red;margin-left:60px"></font></br>
          <font id="notice1" class="hide" style="color:red;margin-left:60px"><img src='${ctx }/img/alarm.png' style='margin-top: -3px'/>请先完成验证</font></br>
            <label>用户名：</label>
            <input id="inputusername" type="text" name="username" class="form-control" placeholder="请输入用户名">
          </div>
          <div class="form-group form-inline" style="margin-left: 180px">
            <label>密&nbsp;&nbsp;&nbsp;&nbsp;码：</label>
            <input id="inputpassword" type="password" name="password" class="form-control" placeholder="请输入密码">
          </div>
          <div class="form-group form-inline" id="captcha2" style="margin-left: 180px;width:90px">
             <p id="wait2"></p>
          </div>
          <div class="form-group form-inline" id="captcha2" style="margin-left: 180px;">
             <p id="wait1">正在加载验证......</p>
          </div>
          <button type="submit" id="formvalidation" class="btn GJBlackbutton block full-width m-b hide">验证</button>
          </br>
          <div class="form-group form-inline" style="text-align:center;">
             <p><a class='GJaxl' onclick="getauthorize()"><img src="${ctx}/img/xinlang.png"  style="margin-top:-5px;margin-right:10px"/>使用新浪账号登录</a></p>
          </div>
        </div>
      <div class="modal-footer">
      	  <img id="ladinggif" class="center-block" src="${ctx}/img/loading.gif" style="width:25px;height:25px;"/>
          <button type="button" class="btn btn-default center-block UserLoginButton GJBlackbutton" style="width:150px;color:ivory">登录</button>
      </div>
      </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</body>
<script src="${ctx}/js/jquery.js"></script>
<script src="${ctx}/js/url.js"></script>
<script src="${ctx }/js/gt.js"></script>
<script>
	var result;
    $("#ladinggif").hide();
    $(".UserLoginButton").click(function(){
    	if($("#inputusername").val().replace(/\s/g, "")==""){
    		$("#errormessage").html("<img src='${ctx }/img/alarm.png' style='margin-top: -3px'>用户名不允许为空!");
    	}else if($("#inputpassword").val().replace(/\s/g, "")==""){
    		$("#errormessage").html("<img src='${ctx }/img/alarm.png' style='margin-top: -3px'>密码不允许为空!");
    	}else{
    		$("#formvalidation").click();
        	if(!result){
        		$("#notice1").removeClass("hide").show(); 
                setTimeout(function () {
                     $("#notice1").hide();
                }, 2000);
        	}else{
        		$(".UserLoginButton").hide();
            	$("#ladinggif").show();
            	$.ajax({
         	        type : 'post',
         	        url : url.server+param.commonlogin,
         	        data : {
         	        	username:$("#inputusername").val(),
         	        	password:$("#inputpassword").val()
         	            },
         	        success : function (res){
         	        	setTimeout(function(){
         	        		$(".UserLoginButton").show();
         	 	       		$("#ladinggif").hide();
         	 	        	if(res.status == '1'){
         	 	        		window.location.reload();
         	 	        	}
         					if (res.status == '2') {
         						$("#errormessage").html("<img src='${ctx }/img/alarm.png' style='margin-top: -3px'>用户名或密码错误,请重新输入!");
         	                }
         	        	}, 1000);
         	        }
         	      });
        	}
    	}
    })
    var handler2 = function (captchaObj) {
        $("#formvalidation").click(function (e) {
        $("[data-toggle='tooltip']").tooltip();
        e.preventDefault();
        	result = captchaObj.getValidate();
        });
        captchaObj.appendTo("#captcha2");
        captchaObj.onReady(function () {
        	$("#wait1").hide();
        	$("#loginbutton").attr("disabled", false); 
        });
    };
    $.ajax({
        url: url.server+param.geettestValidation+"?t=" + (new Date()).getTime(),
        type: "get",
        dataType: "json",
        success: function (data) {
            initGeetest({
                gt: data.gt,
                challenge: data.challenge,
                new_captcha: data.new_captcha, // 用于宕机时表示是新验证码的宕机
                offline: !data.success, // 表示用户后台检测极验服务器是否宕机，一般不需要关注
                product: "float", // 产品形式，包括：float，popup
                width: "100%"
            }, handler2);
        }
    });
    
    var getauthorize = function () {
    	var geturl = url.server+param.getauthorizeurl;
		var sendData = {};
    	$.get(geturl, sendData, function(backData) {
			if (backData.status == 1) {
				window.location.href=backData.data;
			} else {
				alert("获取新浪登录信息出错，请稍后重试");
			}
		});
    }
    </script>
